package com.JL.window;

import com.JL.engine.GameWorld;
import com.JL.engine.model.Entity;
import com.JL.engine.model.GameKeyListener;
import com.JL.engine.model.GameMouseDraggedListener;
import com.JL.engine.model.GameMouseListener;


import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Point2D;

public class GameWindow extends JFrame {
    // 创建当前游戏
    private GameWorld gameWorld = new GameWorld();

    public GameWindow(){
        setTitle("坦克大战");
        // 设置窗口大小
        setSize(800,600);
        // 设置窗口位置居中
        setLocationRelativeTo(null);
        // 设置默认关闭方式
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗口不可调整大小
        setResizable(false);

        // 将窗口的默认面板设置为游戏世界
        setContentPane(gameWorld);
        // 设置窗口可见
        setVisible(true);
        // 使游戏世界获得焦点
        gameWorld.requestFocus();
        initGame();
        initInput();
    }

    public void onMousePressed(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity == null||entity.contains(e.getPoint())) {
                    listener.handler(e,entity);
                }
            }
        });
    }

    public void onMouseReleased(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity == null||entity.contains(e.getPoint())) {
                    listener.handler(e,entity);
                }
            }
        });
    }

    public void onMouseMoved(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity == null||entity.contains(e.getPoint())) {
                    listener.handler(e,entity);
                }
            }
        });
    }

    public void onMouseEntered(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseMotionListener(new MouseAdapter() {

            private Point2D point;

            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity.contains(e.getPoint()) && point != null && !entity.contains(point)) {
                    listener.handler(e,entity);
                }
                point = e.getPoint();
            }
        });
    }
    public void onMouseDragged(Entity entity, GameMouseDraggedListener listener){
        gameWorld.addMouseMotionListener(new MouseAdapter() {
            private Point2D point;

            public void mouseDragged(MouseEvent e){
                if(entity==null||entity.contains(e.getPoint())){
                    if(point==null){
                        point = e.getPoint();
                    }
                    listener.handler(e,point,entity);
                }
                point = e.getPoint();
            }
        });
    }
    public  void  onMouseExited(Entity entity, GameMouseListener listener){
        gameWorld.addMouseMotionListener(new MouseAdapter() {
            private Point2D point;
            public void mouseMoved(MouseEvent e){
                if(!entity.contains(e.getPoint()) && point != null && entity.contains(point)){
                    listener.handler(e,entity);
                }
                point = e.getPoint();

            }
        });
    }

    public <E extends MouseEvent>void onMouseWheelMoved(Entity entity, GameMouseListener<E> listener)
    {
        gameWorld.addMouseWheelListener(new MouseAdapter() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (entity==null||entity.contains(e.getPoint())){
                    listener.handler((E)e,entity);
                }
            }
        });
    }
    public void onMouseClicked(Entity entity,GameMouseListener listener){
        gameWorld.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity==null||entity.contains(e.getPoint())){
                    listener.handler(e,entity);
                }
            }
        });
    }
    public void onKey(int keyCode, GameKeyListener listener){
        gameWorld.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode()==keyCode){
                    listener.keyPressed(e);
                }
            }
            public void keyReleased(KeyEvent e){
                if (e.getKeyCode()==keyCode){
                    listener.keyReleased(e);
                }
            }
        });
    }
    public void initGame(){}
    public void initInput(){}
    public GameWorld getGameWorld(){
        return gameWorld;
    }
}
